/*
 * 作者：姚俊
 * 功能：教师的逻辑处理
 * 日期：2015-05-31
 */
package admin.service.member;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Teacher;
import tools.StringDate;
import database.dao.MysqlCrud;

public class TeacherService {
	
	
	/**
	 * @category 用于教师息的修改
	 * @param teacher1 教师修改的内容
	 * @param teacher2 定位教师修改的位置
	 * @return boolean 修改成功，返回true，否则返回flase
	 */
	public boolean updateTeacher(Teacher teacher1,Teacher teacher2) {
		String sql= "update teacher set name=?,user=?,power=? where teaId=?";
		String[] parameter = {	
				String.valueOf(teacher1.getName()),
				String.valueOf(teacher1.getUser()),
				String.valueOf(teacher1.getPower()),
				String.valueOf(teacher2.getTeaId())
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean b= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	
	/**
	 * @category 用于教师的添加
	 * @return boolean 修改成功，返回true，否则返回flase
	 */
	public boolean AddTeacher(Teacher teacher) {
		String sql= "insert into teacher(name,user,password,power,direction) values(?,?,?,?,?)";
		String[] parameter = {	
				String.valueOf(teacher.getName()),
				String.valueOf(teacher.getUser()),
				String.valueOf(teacher.getPassword()),
				String.valueOf(teacher.getPower()),
				String.valueOf(teacher.getDirection()),
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean b= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	
	/**
	 * @category 按教师ID查询教师
	 * @param teacher Teacher 传入教师的ID
	 * @return teach Teacher 返回一个教师对象
	 */	
	public Teacher getTeacher(Teacher teacher)
	{
		Teacher tea=new Teacher();
		String sql = "select * from Teacher where teaId=?";
		String[]  parameter={String.valueOf(teacher.getTeaId())};
        MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0&&lists!=null)
		{	
			Map map=lists.get(0);
			tea=new MapToBean().MapToTeacher(map);
		}
		
		return tea;
	}
	
	
	/**
	 * @category 按分页查询所有教师
	 * @param pageSize int 一个页面要显示pageSize条个数据
	 * @param pageNow int 从pageNow当前的数据来显示
	 * @return teach Teacher 返回一个教师对象
	 */	
	public List<Teacher> getSegregateTeacherByAll(int pageNow,int pageSize)
	{
		
		String sql = "select * from  (select * from Teacher order by direction,degree) as T where T.teaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+" ";
		List<Teacher> teachers=new ArrayList<Teacher>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, null);
		for(int i=0;i<lists.size();i++)
		{	
			Map map=lists.get(i);
			teachers.add(new MapToBean().MapToTeacher(map));
		}
		
		return teachers;
	}	
	
	
	
	/**
	 * @category 按工号和姓名核查数据库是否存在该老师
	 * @param teacher Teacher 传入教师的的姓名和工号
	 * @return boolean
	 */	
	public boolean CheckUserAndName(Teacher teacher)
	{
		boolean b=false;
		String sql = "select * from Teacher where name=? and user=?";
		String[]  parameter={String.valueOf(teacher.getName()),String.valueOf(teacher.getUser())};
        MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists!=null&&lists.size()!=0)
			b=true;
		return b;
	}
	
	
	/**
	 * @category 用于教师密码的修改
	 * @param teacher 教师修改的内容
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean mendTeacherPass(Teacher teacher) {
		String sql= "update teacher set password=? where user=?";
		String[] parameter = {String.valueOf(teacher.getPassword()),String.valueOf(teacher.getUser())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 按照不同的条件查询所有的教师信息
	 * @param pageSize int 一个页面要显示pageSize条个数据
	 * @param pageNow int 从pageNow当前的数据来显示
	 * @return teach Teacher 返回一个教师对象
	 */	
	public List<Teacher> getTeacherByAll(Teacher teacher,String birthday2,String inSubDate2)
	{
		
//		String sql = "select * from Teacher where teaId LIMIT " +
//				""+(pageNow-1)+" ,"+pageSize+"";
		
		String sql="select * from Teacher ";
		List<Teacher> teachers=new ArrayList<Teacher>();
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		System.out.println("fdsafdsafdsafdsa1");
		if(teacher!=null)
		{
			if(teacher.getName()!=null)
			{
				sql1.add(" name like ? ");
				paras.add("%"+String.valueOf(teacher.getName())+"%");
			}
			if(teacher.getBirthday()!=null)
			{
				sql1.add(" birthday>? and birthday<? ");
				paras.add(String.valueOf(teacher.getBirthday()));
				paras.add(birthday2);
			}
			if(teacher.getInSubDate()!=null)
			{
				sql1.add(" inSubDate>? and inSubDate<? ");
				paras.add(String.valueOf(teacher.getInSubDate()));
				paras.add(inSubDate2);
			}
			if(teacher.getUser()!=null)
			{
				sql1.add(" user=? ");
				paras.add(String.valueOf(teacher.getUser()));
			}
			if(teacher.getDuty()!=null)
			{
				sql1.add(" duty=? ");
				paras.add(String.valueOf(teacher.getDuty()));
			}
			if(teacher.getDegree()!=null)
			{
				sql1.add(" degree=? ");
				paras.add(String.valueOf(teacher.getDegree()));
			}
			if(teacher.getOnline()!=null)
			{
				sql1.add(" online=? ");
				paras.add(String.valueOf(teacher.getOnline()));
			}
			if(teacher.getFurStud()!=null)
			{
				sql1.add(" furStud=? ");
				paras.add(String.valueOf(teacher.getFurStud()));
			}
			if(teacher.getTopSchool()!=null)
			{
				sql1.add(" topSchool=? ");
				paras.add(String.valueOf(teacher.getTopSchool()));
			}
			if(teacher.getTopMajor()!=null)
			{
				sql1.add(" topMajor=? ");
				paras.add(String.valueOf(teacher.getTopMajor()));
			}
			if(teacher.getDirection()!=null)
			{
				sql1.add(" direction=? ");
				paras.add(String.valueOf(teacher.getDirection()));
			}
			
		}
		for(int i=0;i<sql1.size();i++)
		{
			if(i==0)
				sql+="where"+sql1.get(0);
			else
				sql+="and"+sql1.get(i);
		}
		System.out.println("fdsafdsafdsafdsa2");
		String[] parameter=new String[paras.size()];
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		System.out.println("fdsafdsafdsafdsa3");
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<lists.size();i++)
		{	
			Map map=lists.get(i);
			teachers.add(new MapToBean().MapToTeacher(map));
		}
		
		return teachers;
	}	
	
	
	/**
	 * @category 按照不同的条件查询所有的教师的数量
	 * @return int 返回一个数量
	 */	
	public int getCountTeacherByAll(Teacher teacher)
	{
		int count=0;
		String sql="select count(*) from Teacher ";
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		if(teacher!=null)
		{
			if(teacher.getName()!=null)
			{
				sql1.add(" name like ? ");
				paras.add("%"+String.valueOf(teacher.getName())+"%");
			}
			if(teacher.getUser()!=null)
			{
				sql1.add(" user=? ");
				paras.add(String.valueOf(teacher.getUser()));
			}
			if(teacher.getDuty()!=null)
			{
				sql1.add(" duty=? ");
				paras.add(String.valueOf(teacher.getDuty()));
			}
			if(teacher.getDegree()!=null)
			{
				sql1.add(" degree=? ");
				paras.add(String.valueOf(teacher.getDegree()));
			}
			if(teacher.getOnline()!=null)
			{
				sql1.add(" online=? ");
				paras.add(String.valueOf(teacher.getOnline()));
			}
			if(teacher.getFurStud()!=null)
			{
				sql1.add(" furStud=? ");
				paras.add(String.valueOf(teacher.getFurStud()));
			}
			if(teacher.getTopSchool()!=null)
			{
				sql1.add(" topSchool=? ");
				paras.add(String.valueOf(teacher.getTopSchool()));
			}
			if(teacher.getTopMajor()!=null)
			{
				sql1.add(" topMajor=? ");
				paras.add(String.valueOf(teacher.getTopMajor()));
			}
			if(teacher.getDirection()!=null)
			{
				sql1.add(" direction=? ");
				paras.add(String.valueOf(teacher.getDirection()));
			}
			
		}
		for(int i=0;i<sql1.size();i++)
		{
			if(i==0)
				sql+="where"+sql1.get(0);
			else
				sql+="and"+sql1.get(i);
		}
		String[] parameter=new String[paras.size()];
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, parameter);
		count=Integer.parseInt(String.valueOf(lists.get(0).get(0)));
		return count;
	}	
	
	/**
	 * @category 用于删除教师的信息
	 * @param teacher 将要删除的教师信息,改对象中要有教师的ID
	 * @return 删除成功返回true，否则为false
	 */
	public boolean DeleteTeacher(Teacher teacher){
		boolean b=false;
		String sql = "delete from teacher where teaId=?";
		String[]  parameter={String.valueOf(teacher.getTeaId())};
		 MysqlCrud mysqlcurd=new MysqlCrud();
		 b= mysqlcurd.InsertAndUpdateData(sql, parameter);
		 return b;
	}
		

}
